home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / lisp / scroll-bar.el < prev    next >
Lisp/Scheme  |  1993-07-23  |  6KB  |  186 lines

  1. ;;; scroll-bar.el --- window system-independent scroll bar support.
  2.  
  3. ;;; Copyright (C) 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: hardware
  7.  
  8. ;;; This file is part of GNU Emacs.
  9.  
  10. ;;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;;; it under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 2, or (at your option)
  13. ;;; any later version.
  14.  
  15. ;;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;;; GNU General Public License for more details.
  19.  
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Code:
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; Window-system-independent bindings of mouse clicks on the scroll bar.
  29. ;; Presently emulates the scroll-bar behavior of xterm.
  30. ;;; Code:
  31.  
  32. (require 'mouse)
  33.  
  34.  
  35. ;;;; Utilities.
  36.  
  37. (defun scroll-bar-scale (num-denom whole)
  38.   "Given a pair (NUM . DENOM) and WHOLE, return (/ (* NUM WHOLE) DENOM).
  39. This is handy for scaling a position on a scroll bar into real units,
  40. like buffer positions.  If SCROLL-BAR-POS is the (PORTION . WHOLE) pair
  41. from a scroll bar event, then (scroll-bar-scale SCROLL-BAR-POS
  42. \(buffer-size)) is the position in the current buffer corresponding to
  43. that scroll bar position."
  44.   ;; We multiply before we divide to maintain precision.
  45.   ;; We use floating point because the product of a large buffer size
  46.   ;; with a large scroll bar portion can easily overflow a lisp int.
  47.   (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom))))
  48.  
  49.  
  50. ;;;; Helpful functions for enabling and disabling scroll bars.
  51.  
  52. (defun scroll-bar-mode (flag)
  53.   "Toggle display of vertical scroll bars on each frame.
  54. This command applies to all frames that exist and frames to be
  55. created in the future.
  56. With a numeric argument, if the argument is negative,
  57. turn off scroll bars; otherwise, turn on scroll bars."
  58.   (interactive "P")
  59.  
  60.   ;; Obtain the current setting by looking at default-frame-alist.
  61.   (let ((scroll-bar-mode
  62.      (let ((assq (assq 'vertical-scroll-bars default-frame-alist)))
  63.        (if assq (cdr assq) t))))
  64.  
  65.     ;; Tweedle it according to the argument.
  66.     (setq scroll-bar-mode (if (null flag) (not scroll-bar-mode)
  67.                 (or (not (numberp flag)) (>= flag 0))))
  68.  
  69.     ;; Apply it to default-frame-alist.
  70.     (mapcar
  71.      (function
  72.       (lambda (param-name)
  73.     (let ((parameter (assq param-name default-frame-alist)))
  74.       (if (consp parameter)
  75.           (setcdr parameter scroll-bar-mode)
  76.         (setq default-frame-alist
  77.           (cons (cons param-name scroll-bar-mode)
  78.             default-frame-alist))))))
  79.      '(vertical-scroll-bars horizontal-scroll-bars))
  80.  
  81.     ;; Apply it to existing frames.
  82.     (let ((frames (frame-list)))
  83.       (while frames
  84.     (modify-frame-parameters
  85.      (car frames)
  86.      (list (cons 'vertical-scroll-bars scroll-bar-mode)
  87.            (cons 'horizontal-scroll-bars scroll-bar-mode)))
  88.     (setq frames (cdr frames))))))
  89.  
  90. ;;;; Buffer navigation using the scroll bar.
  91.  
  92. ;;; This was used for up-events on button 2, but no longer.
  93. (defun scroll-bar-set-window-start (event)
  94.   "Set the window start according to where the scroll bar is dragged.
  95. EVENT should be a scroll bar click or drag event."
  96.   (interactive "e")
  97.   (let* ((end-position (event-end event))
  98.      (window (nth 0 end-position))
  99.      (portion-whole (nth 2 end-position)))
  100.     (save-excursion
  101.       (set-buffer (window-buffer window))
  102.       (save-excursion
  103.     (goto-char (+ (point-min)
  104.               (scroll-bar-scale portion-whole
  105.                     (- (point-max) (point-min)))))
  106.     (beginning-of-line)
  107.     (set-window-start window (point))))))
  108.  
  109. ;; Scroll the window to the proper position for EVENT.
  110. (defun scroll-bar-drag-1 (event)
  111.   (let* ((start-position (event-start event))
  112.      (window (nth 0 start-position))
  113.      (portion-whole (nth 2 start-position)))
  114.     (save-excursion
  115.       (set-buffer (window-buffer window))
  116.       ;; Calculate position relative to the accessible part of the buffer.
  117.       (goto-char (+ (point-min)
  118.             (scroll-bar-scale portion-whole
  119.                       (- (point-max) (point-min)))))
  120.       (beginning-of-line)
  121.       (set-window-start window (point)))))
  122.  
  123. (defun scroll-bar-drag (event)
  124.   "Scroll the window by dragging the scroll bar slider.
  125. If you click outside the slider, the window scrolls to bring the slider there."
  126.   (interactive "e")
  127.   (let* (done)
  128.     (scroll-bar-drag-1 event)
  129.     (track-mouse
  130.       (while (not done)
  131.     (setq event (read-event))
  132.     (if (eq (car-safe event) 'mouse-movement)
  133.         (setq event (read-event)))
  134.     (cond ((eq (car-safe event) 'scroll-bar-movement)
  135.            (scroll-bar-drag-1 event))
  136.           (t
  137.            ;; Exit when we get the drag event; ignore that event.
  138.            (setq done t)))))))
  139.  
  140. (defun scroll-bar-scroll-down (event)
  141.   "Scroll the window's top line down to the location of the scroll bar click.
  142. EVENT should be a scroll bar click."
  143.   (interactive "e")
  144.   (let ((old-selected-window (selected-window)))
  145.     (unwind-protect
  146.     (progn
  147.       (let* ((end-position (event-end event))
  148.          (window (nth 0 end-position))
  149.          (portion-whole (nth 2 end-position)))
  150.         (select-window window)
  151.         (scroll-down
  152.          (scroll-bar-scale portion-whole (1- (window-height))))))
  153.       (select-window old-selected-window))))
  154.  
  155. (defun scroll-bar-scroll-up (event)
  156.   "Scroll the line next to the scroll bar click to the top of the window.
  157. EVENT should be a scroll bar click."
  158.   (interactive "e")
  159.   (let ((old-selected-window (selected-window)))
  160.     (unwind-protect
  161.     (progn
  162.       (let* ((end-position (event-end event))
  163.          (window (nth 0 end-position))
  164.          (portion-whole (nth 2 end-position)))
  165.         (select-window window)
  166.         (scroll-up
  167.          (scroll-bar-scale portion-whole (1- (window-height))))))
  168.       (select-window old-selected-window))))
  169.  
  170.  
  171. ;;;; Bindings.
  172.  
  173. ;;; For now, we'll set things up to work like xterm.
  174. (global-set-key [vertical-scroll-bar mouse-1] 'scroll-bar-scroll-up)
  175. (global-set-key [vertical-scroll-bar drag-mouse-1] 'scroll-bar-scroll-up)
  176.  
  177. (global-set-key [vertical-scroll-bar down-mouse-2] 'scroll-bar-drag)
  178.  
  179. (global-set-key [vertical-scroll-bar mouse-3] 'scroll-bar-scroll-down)
  180. (global-set-key [vertical-scroll-bar drag-mouse-3] 'scroll-bar-scroll-down)
  181.  
  182.  
  183. (provide 'scroll-bar)
  184.  
  185. ;;; scroll-bar.el ends here
  186.